<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title>客户发展计划</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi"/>
    <link rel="shortcut icon" href="/favicon.ico" type="image/x-icon"/>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/static/css/font.css">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/static/css/xadmin.css">
    <script type="text/javascript" src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
    <script type="text/javascript" src="${pageContext.request.contextPath}/static/lib/layui/layui.js"
            charset="utf-8"></script>
    <script type="text/javascript" src="${pageContext.request.contextPath}/static/js/xadmin.js"></script>
    <script type="text/javascript" src="${pageContext.request.contextPath}/static/js/xm-select.js"></script>
    <!-- 让IE8/9支持媒体查询，从而兼容栅格 -->
    <!--[if lt IE 9]>
    <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
    <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
</head>

<body>
<div class="x-nav">
      <span class="layui-breadcrumb">
        <a href="">首页</a>
        <a href="">客户发展计划</a>
        <a>
          <cite>列表</cite></a>
      </span>
    <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right"
       href="javascript:location.replace(location.href);" title="刷新">
        <i class="layui-icon" style="line-height:40px">ဂ</i></a>
</div>
<div class="x-body">
    <div class="layui-row">
        <form class="layui-form layui-col-md12 x-so" onsubmit="return false;">
            月份：
            <div id="months" class="layui-input-inline" style="width: 800px"></div>
            <button class="layui-btn" id="search-btn"><i class="layui-icon">&#xe615; 查询</i></button>
        </form>
    </div>
    <xblock class="do-table">
        <button class="layui-btn"
                onclick="x_admin_show('添加发展计划','${pageContext.request.contextPath}/cm/devplan/add/page')"><i
                class="layui-icon"></i>添加
        </button>
        <button class="layui-btn layui-btn-danger" data-type="batchDelete"><i class="layui-icon"></i>批量删除</button>
    </xblock>
    <table id="data" lay-filter="devplan"></table>

</div>
<script>
    var table;
    layui.use(['laydate', 'table'], function () {
        var laydate = layui.laydate;
        table = layui.table;
        //执行一个laydate实例
        laydate.render({
            elem: '#start' //指定元素
        });
        //执行一个laydate实例
        laydate.render({
            elem: '#end' //指定元素
        });

        table.render({
            elem: '#data'
            , height: 600
            , url: '${pageContext.request.contextPath}/cm/devplan/list/data' //数据接口
            , method: "post"
            , page: {limit: 2, limits: [2, 5, 10, 20, 30, 40, 50]} //开启分页
            , cols: [[ //表头，列宽总和最多98%，否则出现水平滚动条
                {type: 'checkbox', fixed: 'left', width: '5%'}
                , {type: 'numbers', title: '序号', width: '7%', sort: true}
                , {
                    field: 'createEmp', title: '姓名', width: '10%', sort: true, templet: function (d) {
                        return d.createEmp.emplRealName
                    }
                }
                , {
                    field: 'month', title: '月份', width: '8%', sort: true, templet: function (d) {
                        let tmp = d.createTime;
                        return tmp.slice(0, tmp.lastIndexOf('-'))
                    }
                }
                , {field: 'createTime', title: '创建时间', width: '20%', sort: true}
                , {field: 'updateTime', title: '最后操作时间', width: '20%', sort: true}
                , {
                    field: 'updateEmp', title: '操作人', width: '10%', templet: function (d) {
                        return d.updateEmp.emplRealName
                    }
                }
                , {fixed: 'right', title: '操作', width: '18%', align: 'center', toolbar: '#do'}
            ]]
        });
        //工具条事件
        table.on('tool(devplan)', function (obj) { //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            var tr = obj.tr; //获得当前行 tr 的 DOM 对象（如果有的话）

            if (layEvent === 'status') { //改变状态
                // 先改变状态
                data.status = data.status === 0 ? 1 : 0;
                var devplanData = {'id': data.id, 'status': data.status};
                // 发送ajax改变状态
                $.ajax({
                    type: "post",
                    url: "${pageContext.request.contextPath}/cm/devplan/updateStatus",
                    data: devplanData,
                    dataType: "json",
                    success: function (res) {
                        if (res.code === 200) {
                            // 重新加载表格
                            table.reload("data");
                        }
                    }
                });

            } else if (layEvent === 'detail') { //编辑
                // 查看
                x_admin_show('查看计划详情', '${pageContext.request.contextPath}/cm/devplan/detail/page?id=' + data.id)
            } else if (layEvent === 'edit') {
                // 编辑
                x_admin_show('修改计划', '${pageContext.request.contextPath}/cm/devplan/edit/page?id=' + data.id)
            } else if (layEvent === 'delete') {
                // 删除单行
                layer.confirm('真的删除行么', function (index) {
                    obj.del(); //删除对应行（tr）的DOM结构，并更新缓存
                    layer.close(index);
                    // 发送请求到后端
                    $.ajax({
                        type: "post",
                        url: "${pageContext.request.contextPath}/cm/devplan/batchDelete",
                        data: {ids: [data.id]},
                        dataType: "json",
                        success: function (res) {
                            layer.msg(res.msg, {icon: 6, time: 500}, function () {
                                // 刷新父级的表格
                                table.reload('data');
                                // window.location.reload();
                            });
                        }
                    });
                });
            }
        });

        var $ = layui.$, active = {
            batchDelete: function () {
                var checkStatus = table.checkStatus('data')
                    , data = checkStatus.data;
                if (data.length === 0) {
                    layer.msg('请至少选中一行数据，进行删除！');
                    return;
                }
                layer.confirm('确认要删除吗？', function (index) {
                    //获取选中数据中的id并组装成一个数组
                    var ids = [];
                    $(data).each(function () {
                        ids.push(this.id);
                    });
                    // 发送请求到后端
                    $.ajax({
                        type: "post",
                        url: "${pageContext.request.contextPath}/cm/devplan/batchDelete",
                        data: {ids: ids},
                        dataType: "json",
                        success: function (res) {
                            layer.msg(res.msg, {icon: 6, time: 500}, function () {
                                // 刷新父级的表格
                                table.reload('data');
                                // window.location.reload();
                            });
                        }
                    });
                });
            }
        };

        $('.do-table .layui-btn-danger').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

        // 搜索按钮
        document.getElementById('search-btn').onclick = function () {
            //获取当前多选选中的值
            let selectedMonths = months.getValue('value');
            console.log(selectedMonths);
            // 组装总共的提交参数
            let queryParam = {};
            queryParam.selectedMonths = selectedMonths;
            console.log(queryParam);
            // 表格数据的重载
            table.reload("data", {where: queryParam})
        };
    });
</script>

<script type="text/html" id="do">
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="detail">查看</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="delete">删除</a>
</script>

<script>
    var months = xmSelect.render({
        el: '#months',
        language: 'zn',
        tips: '不限',
        data: [
            {name: '一月', value: '01'},
            {name: '二月', value: '02'},
            {name: '三月', value: '03'},
            {name: '四月', value: '04'},
            {name: '五月', value: '05'},
            {name: '六月', value: '06'},
            {name: '七月', value: '07'},
            {name: '八月', value: '08'},
            {name: '九月', value: '09'},
            {name: '十月', value: '10'},
            {name: '十一月', value: '11'},
            {name: '十二月', value: '12'}
        ]
    })
</script>

</body>
</html>